using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace BlancoWorld.EventReceiversManager
{
    public class EventReceiverCollectionBinder 
    {
        protected const string EVENT_RECEIVER_ID_COLUMN = "EventReceiverID";
        protected const string EVENT_RECEIVER_TYPE_COLUMN = "EventReceiverType";
        protected const string EVENT_RECEIVER_ASSEMBLY_COLUMN = "EventReceiverAssembly";
        protected const string EVENT_RECEIVER_CLASS_NAME_COLUMN = "EventReceiverClassName";
        protected const string EVENT_RECEIVER_SEQUENCE_COLUMN = "EventReceiverSequence";

        protected DataTable _eventReceiversCollection = new DataTable();
        protected DataView _eventReceiversView = null;
        protected EventReceiversManagerPage _page = null;
        protected Guid _listId = Guid.Empty;

        public DataTable EventReceivers
        {
            get
            {
                return _eventReceiversCollection;
            }
        }

        public EventReceiverCollectionBinder(EventReceiversManagerPage page, Guid listId)
        {
            _page = page;
            _listId = listId;
            _eventReceiversCollection.Columns.Add(EVENT_RECEIVER_ID_COLUMN, typeof(string));
            _eventReceiversCollection.Columns.Add(EVENT_RECEIVER_TYPE_COLUMN, typeof(string));
            _eventReceiversCollection.Columns.Add(EVENT_RECEIVER_ASSEMBLY_COLUMN, typeof(string));
            _eventReceiversCollection.Columns.Add(EVENT_RECEIVER_CLASS_NAME_COLUMN, typeof(string));
            _eventReceiversCollection.Columns.Add(EVENT_RECEIVER_SEQUENCE_COLUMN, typeof(Int32));
        }

        public void AddEventReceiver(Guid id, string type, string assembly, string className, int sequence) 
        {
            DataRow newRow = _eventReceiversCollection.Rows.Add();
            newRow[EVENT_RECEIVER_ID_COLUMN] = _page.Server.UrlEncode(id.ToString());
            newRow[EVENT_RECEIVER_TYPE_COLUMN] = type;
            newRow[EVENT_RECEIVER_ASSEMBLY_COLUMN] = assembly;
            newRow[EVENT_RECEIVER_CLASS_NAME_COLUMN] = className;
            newRow[EVENT_RECEIVER_SEQUENCE_COLUMN] = sequence;
        }

        public void SortGrid(SPGridView grid, string sortExpression)
        {
            _eventReceiversView.Sort = sortExpression;
            grid.DataBind();
        }

        public void BindGrid(SPGridView grid, string sourceUrl) 
        {
            _eventReceiversView = new DataView(_eventReceiversCollection);

            BoundField fldEventType = new BoundField();
            fldEventType.HeaderText = "Type";
            fldEventType.DataField = EVENT_RECEIVER_TYPE_COLUMN;
            grid.Columns.Add(fldEventType);
            fldEventType.Visible = false;

            // --- The assembly menu column - offers a drop down to edit and delete event receivers
            SPMenuField assemblyMenuColumn = new SPMenuField();
            assemblyMenuColumn.HeaderText = "Assembly";
            assemblyMenuColumn.TextFields = EVENT_RECEIVER_ASSEMBLY_COLUMN;
            assemblyMenuColumn.MenuTemplateId = "AssemblyMenu";
            assemblyMenuColumn.TokenNameAndValueFields = String.Format("EVENTRECID={0}", EVENT_RECEIVER_ID_COLUMN);
            assemblyMenuColumn.SortExpression = EVENT_RECEIVER_ASSEMBLY_COLUMN;

            MenuTemplate assemblyTypeMenu = new MenuTemplate();
            assemblyTypeMenu.ID = "AssemblyMenu";

            MenuItemTemplate editMenuItem = new MenuItemTemplate(
                "Edit", "/_layouts/images/EDITITEM.gif");
            editMenuItem.ClientOnClickNavigateUrl = String.Format(
                "EditEventReceiver.aspx?ListId={0}&EvRecId=%EVENTRECID%&SourceUrl={1}", 
                _page.Server.UrlEncode(_listId.ToString()), _page.Server.UrlEncode(sourceUrl));
            assemblyTypeMenu.Controls.Add(editMenuItem);

            MenuItemTemplate deleteMenuItem = new MenuItemTemplate(
                "Delete", "/_layouts/images/DELETE.gif");
            deleteMenuItem.ClientOnClickNavigateUrl = String.Format(
                "DeleteEventReceiver.aspx?ListId={0}&EvRecId=%EVENTRECID%&SourceUrl={1}",
                _page.Server.UrlEncode(_listId.ToString()), _page.Server.UrlEncode(sourceUrl));
            assemblyTypeMenu.Controls.Add(deleteMenuItem);

            _page.Controls.Add(assemblyTypeMenu);
            grid.Columns.Add(assemblyMenuColumn);
            // ---

            BoundField fldClassName = new BoundField();
            fldClassName.HeaderText = "Class";
            fldClassName.DataField = EVENT_RECEIVER_CLASS_NAME_COLUMN;
            grid.Columns.Add(fldClassName);

            BoundField fldSequence = new BoundField();
            fldSequence.HeaderText = "Sequence";
            fldSequence.DataField = EVENT_RECEIVER_SEQUENCE_COLUMN;
            grid.Columns.Add(fldSequence);

            grid.DataSource = _eventReceiversView;
            grid.AutoGenerateColumns = false;
            grid.DataBind();
        }
    }
}
